<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"
	xmlns:xf="http://www.w3.org/2002/xforms"
	exclude-result-prefixes="xsl ccts">
	
	<xsl:include href="instances.xsl" />
	<xsl:include href="bindings.xsl" />
	<xsl:include href="gui.xsl" />
	<xsl:include href="style-css.xsl" />
	
	<xsl:output media-type="application/xhtml+xml" method="xml" 
		encoding="UTF-8" version="1.0" indent="yes" omit-xml-declaration="yes"/>
	
	<!--
		Three things there have to be done:
		1. Create the XForms instance
		2. Create the appropriate binding statements
		3. Create the XForms ui
	-->
	<xsl:template match="xsd:schema">
		<html>
			<!-- 
				Message namespace should be declared in the schema with the prefix 'rsm' according to the
				naming and design rules.
			-->
			<xsl:copy-of select="namespace::ccts" />
			<xsl:copy-of select="namespace::udt" />
			<xsl:copy-of select="namespace::qdt" />
			<xsl:copy-of select="namespace::ram" />
			<xsl:copy-of select="namespace::rsm" />
			
			<head>
				<title><xsl:value-of select="/xsd:schema/xsd:element[1]/xsd:annotation/xsd:documentation/ccts:Name"></xsl:value-of></title>
				
				<!--
				<xsl:element name="object">
					<xsl:attribute name="id">FormsPlayer</xsl:attribute>
					<xsl:attribute name="classid">CLSID:4D0ABA11-C5F0-4478-991A-375C4B648F58</xsl:attribute>
					<xsl:attribute name="width">0</xsl:attribute>
					<xsl:attribute name="height">0</xsl:attribute>
					<b>FormsPlayer has failed to load! Please check your installation.</b>
				</xsl:element>
				<xsl:processing-instruction name="import">namespace="xf" implementation="#FormsPlayer"</xsl:processing-instruction>
				-->
				
				<xsl:apply-templates select="/xsd:schema/xsd:element[1]" mode="style" />
				
				<xsl:element name="xf:model">
					<xsl:attribute name="schema" select="document-uri(/)" />
					<!--
					<xsl:apply-templates select="/xsd:schema/xsd:element[1]" mode="instances"  />
					<xsl:apply-templates select="/xsd:schema/xsd:element[1]" mode="bindings"  />
					-->
					
					<xsl:variable name="document" select="concat( /xsd:schema/xsd:element/@name, '.xml' )"/>
					<!-- Submission -->
					<xsl:element name="xf:submission">
						<xsl:attribute name="id">message</xsl:attribute>
						<xsl:attribute name="method">put</xsl:attribute>
						<xsl:attribute name="action" select="$document" />
						<xsl:attribute name="includenamespaceprefixes">#default</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</head>
			<body>
				<!--<xsl:apply-templates select="/xsd:schema/xsd:element[1]" mode="gui-elements"  /> -->
				<div>
					<xf:submit submission="message">
						<xf:label>Submit</xf:label>
					</xf:submit>
				</div>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="xsd:element" mode="style">
		<xsl:call-template name="generate-style" >
			<xsl:with-param name="element.root" select="@name" />
			<xsl:with-param name="element.root.type" select="@type" />
		</xsl:call-template>
	</xsl:template>
	
	<!-- 
		Creates the instances for the XForms document. This is in the first place the instance
		for the message to send. And in the second place the instances that refer to the codelists
		that are used in the message schema.
		
		@template build-instances - see Modules/instances.xsl
	-->
	<xsl:template match="xsd:element" mode="instances">
		<xsl:call-template name="GenerateInstances">
			<xsl:with-param name="element.root" select="@name" />
			<xsl:with-param name="element.root.prefix" select="$global.schema.root.prefix" />
		</xsl:call-template>
	</xsl:template>
	
	<!-- 
		Creates the bindings in the XForms model. It binds all nodes in the message
		too the datatype as defined in the schema.
		
		@template build-bindings - see Modules/bindings.xsl
	-->
	<xsl:template match="xsd:element" mode="bindings">
		<xsl:call-template name="GenerateBindings">
			<xsl:with-param name="element.root" select="@name" />
			<xsl:with-param name="element.root.type" select="@type" />
		</xsl:call-template>
	</xsl:template>
	
	<!-- 
		Creates the gui elements of the XForms document.
		
		@template build-gui - see Modules/gui.xsl
	-->
	<xsl:template match="xsd:element" mode="gui-elements">
		<xsl:call-template name="GenerateGui">
			<xsl:with-param name="type" select="@type" />
			<xsl:with-param name="prefix"><xsl:value-of select="$global.schema.root.prefix" /></xsl:with-param>
		</xsl:call-template>
	</xsl:template>
</xsl:stylesheet>